﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Windows.Controls.DataVisualization;

using System.ComponentModel;
using ActivityTracker.Library;
using Common.Library;

namespace ActivityTracker
{
    public partial class Monthly_CategoryBarChart : PhoneApplicationPage
    {
       
        #region Global

        Charts objCharts = new Charts();
        MonthlyChart objMonthlyChart = new MonthlyChart();
        string month = "";
        string year = "";

        #endregion
        public Monthly_CategoryBarChart()
        {
            InitializeComponent();
            BarChart.LegendStyle = ChartLegendStyles.hidelegend();
            BarChart.Visibility = Visibility.Collapsed;
        }
     

        #region page events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lpicMonth.SelectedItem == null)
                {
                lpicMonth.ItemsSource = objCharts.GetMonths();
                lpicYear.ItemsSource = objCharts.GetYears();
                lpicMonth.SelectedIndex = Convert.ToInt32(DateTime.Now.ToString("MM")) - 1;
                }
                else
                {
                    lpicMonth.SelectedIndex = lpicMonth.SelectedIndex;
                }
                lpicYear.SelectedItem = (DateTime.Now.Year.ToString());
                //if (NavigationContext.QueryString.TryGetValue("SelectedYearindex", out SelectedYearindex) && NavigationContext.QueryString.TryGetValue("SelectedMonthindex", out SelectedMonthindex))
                //{
                //    lpicYear.SelectedIndex = Convert.ToInt32(SelectedYearindex);
                //    lpicMonth.SelectedIndex = Convert.ToInt32(SelectedMonthindex);
                //}
                year = lpicYear.SelectedItem.ToString();
                month = lpicMonth.SelectedItem.ToString();
                LoadChart(month, year);
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In Monthly_TimeSpentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnok_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                month = lpicMonth.SelectedItem.ToString();
                year = lpicYear.SelectedItem.ToString();
                LoadChart(month, year);
            }
            catch (Exception ex)
            {
                string mess = "Exception in btnok_Click Method In Monthly_TimeSpentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnPieChart_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Monthly/Monthly_CategoryPieChart.xaml?SelectedYearindex=" + lpicYear.SelectedIndex.ToString() + "&SelectedMonthindex=" + lpicMonth.SelectedIndex.ToString(), UriKind.Relative));
        }
        //private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/Views/Charts/Monthly/Monthly_TimeSpentBarChart.xaml", UriKind.Relative));
        //}

        #endregion

        #region Custom methods

        private void LoadChart(string month, string year)
        {
            try
            {
                List<ChartProp> objChartList = objMonthlyChart.LoadCategoryTimeSpentData(month, year, ChartType.Bar);
                if (objChartList.Count > 0)
                    txtblkResult.Visibility = Visibility.Collapsed;
                else
                    txtblkResult.Visibility = Visibility.Visible;
                ContentPanel.DataContext = objChartList;
                BarChart.Height = (objChartList.Count * 50) + 50;
                BarSeries b = (BarSeries)BarChart.Series[0];
                CategoryAxis ca = new CategoryAxis();
                ca.Height = objChartList.Count * 50;
                b.IndependentAxis.DependentAxes.Add(ca);
                BarChart.Style = Application.Current.Resources["PhoneChartStyle"] as Style;
                BarChart.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadChart Method In Monthly_TimeSpentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
        }

        #endregion

       
    }
}